package com.relatev.minecraft.TRMobAPI;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class ReflectionUtils {

    public static Field getDeclaredFieldIncludingParents(Class clazz, String fn) {
        for (Field field : clazz.getDeclaredFields()) {
            if (field.getName().equals(fn)) {
                return field;
            }
        }
        Class superclass = clazz;
        while (superclass.getSuperclass() != null) {
            superclass = superclass.getSuperclass();
            for (Field field : superclass.getDeclaredFields()) {
                if (field.getName().equals(fn)) {
                    return field;
                }
            }
        }
        return null;
    }

    public static List<Field> getDeclaredFieldsIncludingParents(Class clazz) {
        List<Field> fields = new ArrayList<>();
        for (Field field : clazz.getDeclaredFields()) {
            fields.add(field);
        }
        Class superclass = clazz;
        while (superclass.getSuperclass() != null) {
            superclass = superclass.getSuperclass();
            for (Field field : superclass.getDeclaredFields()) {
                fields.add(field);
            }
        }
        return fields;
    }
}
